<?php

class AnalysisModel extends Model
{
    //同步事件信息数据
    function _getEvent($member_id = 0, $data)
    {
        $analysis = M('Analysis');
        $map['member_id'] = $member_id;
        $info = $this->where($map)->select();
        if(empty($info))
        {
            $infos['member_id'] = $member_id;
            $infos['enumber'] = $data['integralnumber'];
            $infos['integral_type'] = $data['integraltypeid'];
            $infos['time'] = $data['datetime'];
            $infos['site_id'] = __SITEID__;
            $infos['oldid'] = $data['oldid'];
            $analysis->add($infos);
            exit;
        }
        $count = 0;
        foreach($info as $key => $value) {
            if(date('Y-m-d', $data['datetime']) == date('Y-m-d', $value['time'])) {
                $value['enumber'] += $data['integralnumber'];
                $value['time'] = $data['datetime'];
                $analysis->where(array('id' => $value['id']))->save($value);
                $count += 1;
            }
        }
        if($count == 0 && !empty($info)){
                $infos['member_id'] = $member_id;
                $infos['enumber'] = $data['integralnumber'];
                $infos['integral_type'] = $data['integraltypeid'];
                $infos['time'] = $data['datetime'];
                $infos['site_id'] = __SITEID__;
                $infos['oldid'] = $data['oldid'];
                $analysis->add($infos);
        }
    }
	
	public function lwnranking(){
		   $pid=$this->memberInfo['id'];
        $member=$this->dao->where('pid = '.$pid)->select();
        foreach($member as $key => $val){
            $special=D('Integralranking')->where('member_id = '.$val['id'])->group("FROM_UNIXTIME(`dateline` + 8 * 3600,'%Y-%m')")->field("sum(num) as sum,FROM_UNIXTIME(`dateline` + 8 * 3600,'%Y年%m月') as date")->select();
            $special=array_slice($special,'-2');
            if(count($special)==2){//至少有两个月的积分才能比较
                $rate[$val['id']]=$special['1']['sum']/$special['0']['sum'];
            }
        }
	
        arsort($rate);//键值倒序
        //var_dump($rate);exit; //1.24   1.14
        if(current(array_values($rate))>=1.20){//rate>4/3 正变动最大且大于4/3 上升异常
            $up_member=current(array_keys($rate));//member_id
            $up_rate=current(array_values($rate));//
        }
        if(end(array_values($rate))<=1.15){//rate<3/4 负変动最大  下滑异常
            $down_member=end(array_keys($rate));
            $down_rate=end(array_values($rate));
        }
		
		    if(!empty($up_rate)){//有上升异常
            $up_detail=D('Integralranking')->where('member_id = '.$up_member)->group("FROM_UNIXTIME(`dateline` + 8 * 3600,'%Y-%m')")->field("sum(num) as sum,FROM_UNIXTIME(`dateline` + 8 * 3600,'%Y年%m月') as date")->select();
            $up_arr=array();
            $up_date=array();
            foreach($up_detail as $key => $val){
                $up_arr[]=intval($val['sum']);//单日分数
                $up_date[]=$val['date'];//日期
            }
            $this->assign('up_arr',json_encode($up_arr));
            $this->assign('up_date',json_encode($up_date));
            $up=D('Member')->where('id ='.$up_member)->find();
            $this->assign('up_member',$up_member);
            $this->assign('up_person',$up['name']);
        }
	}
	public function week($time){
		$time=$time?$time:time();
		$time=date("Y-m-d",$time);
		$lastday=date("Y-m-d",strtotime("$time Sunday"));
		$arr['start']=abs(strtotime("$lastday - 6 days"));
		$arr['end']=strtotime($lastday);
		$arr['interval']=24*3600;
		$arr['key']='d';
		$arr['format']="Y年m月d日";
		$data=$date=[];
		for($x=$arr['start'];$x<$arr['end'];$x=$x+$arr['interval']){
			$key=intval(date($arr['key'],$x));
			$data[$key]=0;
			$date[$key]=date($arr['format'],$x);
		}
		$arr['data']=$data;
		$arr['date']=$date;
		return $arr;
	}	
    public function month($time){
        $time=$time?$time:time();
        $arr['start']=strtotime(date('Y',$time).'-'.date('m',$time)."-1");
        if(date('m',$time)==12){
            $arr['end']=strtotime(date('Y-m-31 23:59:59',$time));
        }else{
            $arr['end']=strtotime(date('Y',$time).'-'.(date('m',$time)+1).'-1');
        }
		$arr['interval']=24*3600;
		$arr['key']='d';
		$arr['format']="Y年m月d日";
		$data=$date=[];
		for($x=$arr['start'];$x<$arr['end'];$x=$x+$arr['interval']){
			$key=intval(date($arr['key'],$x));
			$data[$key]=0;
			$date[$key]=date($arr['format'],$x);
		}
		$arr['data']=$data;
		$arr['date']=$date;
        return $arr;
    }
	public function year($time){
        $time=$time?$time:time();
        $arr['start']=strtotime(date('Y',$time)."-1-1");//year start unixtime 2016-01-01 00:00:00
        $arr['end']=strtotime((date('Y',$time)+1).'-1-1');
		$arr['interval']=28*24*3600;
		$arr['key']='m';
		$arr['format']="Y年m月";
		$data=$date=[];
		for($x=$arr['start'];$x<$arr['end'];$x=$x+$arr['interval']){
			$key=intval(date($arr['key'],$x));
			$data[$key]=0;
			$date[$key]=date($arr['format'],$x);
		}
		$arr['data']=$data;
		$arr['date']=$date;
        return $arr;
    }
	//部门折线图
    public function chart($mid,$gradetype,$time,$map=[]){
        $arr=array();
        $date=array();
        $newarr=array();
		$map=$map;
		$integralDB=D('Integral_log');
		if($mid){
        	$map['member_id']=['in',$mid];
		}
		if(isset($map['memberpositionid'])){
			unset($map['member_id']);
			
		}
        $map["time"]=array('between',array($time['start'],$time['end']));   //periods of time
        $list=$integralDB->where($map)->group("FROM_UNIXTIME(`time` + 8 * 3600,'%Y-%m-%d')")->field("time,sum(changenum) as sum,FROM_UNIXTIME(`time` + 8 * 3600,'%Y年%m月%d日') as date, (`time` + 8 * 3600) as t")->select();
		//dump($integralDB->getLastSQL());exit;
		$arr=$time['data'];
        foreach($list as $key => $val){
			$i=intval(date('d',$val['t']));
            $arr[$i]=abs(intval($val['sum']));//单日分数
        }
		
		//dump($arr);exit;
        $ret['arr']=$arr;
		$ret['sum']=$integralDB->where($map)->sum('changenum');
		$ret['count']=$integralDB->where($map)->count('changenum');
        return $ret;
    }
	//年
    public function chartYear($mid,$gradetype,$time,$map=[]){
		//echo $this->strtotimenext(time(), 2);
		$map=$map;
		$integralDB=D('Integral_log');
		if($mid){
        	$map['member_id']=['in',$mid];
		}
        $map['time']=array('between',array($time['start'],$time['end']));
        $yearlist=$integralDB->where($map)->group("FROM_UNIXTIME(`time` + 8 * 3600,'%Y-%m')")->field("time,sum(changenum) as sum,FROM_UNIXTIME(`time` + 8 * 3600,'%Y年%m月') as date")->select();
        $arr=array();
        $yeardate=array();
		$arr=$time['data'];
        foreach($yearlist as $key => $val){
			$i=intval(date('m',$val['time']));
            $arr[$i]=abs(intval($val['sum']));//单日分数
        }
        $ret['arr']		 =$arr;
		$ret['sum']=$integralDB->where($map)->sum('changenum');
		$ret['count']=$integralDB->where($map)->count('changenum');
        return $ret;
    }
	
	/*//周
    public function chartWeek($mid,$gradetype,$start,$end,$map=[]){
		$map=$map;
		$integralDB=D('Integral_log');
        $map["time"]=array('between',array($start,$end));
        $map['member_id']=['in',$mid];
        $weeklist=$integralDB->where($map)->group("FROM_UNIXTIME(`time` + 8 * 3600,'%v')")->field("sum(changenum) as sum,FROM_UNIXTIME(`time` + 8 * 3600,'%v') as date")->select();
        $weekarr=array();
        $weekdate=array();
        foreach($weeklist as $key => $val){
            $weekarr[]=intval($val['sum']);//单日分数
            $year = date('Y',$start);
            $week = $val['date'];
            $weeks['mon']=strtotime($year.'-W'.$week.'-0')<$start   ? date('m-d',$start):date('m-d',strtotime($year.'-W'.$week.'-0'));
            $weeks['sun']=strtotime($year.'-W'.$week.'-6')>$end     ? date('m-d',$end)  :date('m-d',strtotime($year.'-W'.$week.'-6'));
            $weekdate[]=$weeks['mon'].'至'.$weeks['sun'];//周日至周六
        }
        $weekreturn['arr']=json_encode($weekarr);
        $weekreturn['date']=json_encode($weekdate);
        return $weekreturn;
    }
	
	//部门折线图
    public function chartMonth($mid,$gradetype,$start,$end,$map=[]){
		$map=$map;
		$integralDB=D('Integral_log');
        $map['member_id']=['in',$mid];
        $map["time"]=array('between',array($start,$end));   //periods of time
        $monthlist=$integralDB->where($map)->group("FROM_UNIXTIME(`time` + 8 * 3600,'%Y-%m-%d')")->field("sum(changenum) as sum,FROM_UNIXTIME(`time` + 8 * 3600,'%Y年%m月%d日') as date")->select();
        $montharr=array();
        $monthdate=array();
        foreach($monthlist as $key => $val){
            $montharr[]=intval($val['sum']);//单日分数
            $monthdate[]=$val['date'];//日期
        }
        $tip['i.member_id']=['in',$mid];
        $tip['i.type_id']=$gradetype;
        $tip["i.time"]=$map["time"];   //periods of time
        $monthsql=$integralDB->alias('i')->join('__FORM__ f on f.id = i.form_id')->join('right join __FORMCATEGORY__ c on c.id =f.topid')->where($tip)->group('f.topid')->field('c.name as formname,sum(i.changenum) as  sum')->select();
        $newarr=array();
        foreach($monthsql as $k => $v){
            $newarr[]=array('name'=>$v['formname'],'y'=>intval($v['sum']));
        }
        $monthreturn['form'] =json_encode($newarr);
        $monthreturn['arr']=json_encode($montharr);
        $monthreturn['date']=json_encode($monthdate);
        return $monthreturn;
    }*/
	
	
	
	private function strtotimenext($time, $d = 1){
		return strtotime($d.' day', $time);
	}
	
}